package com.hongye.why.app;

import android.app.Activity;
import android.app.Application;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;

import com.hongye.why.base.ActivityStack;
import com.hongye.why.base.CrashHandler;
import com.hongye.why.module.LoginActivity;
import com.hongye.why.util.BaseUtils;
import com.hongye.why.view.CustomDialog;

/**
 * 应用程序管理
 */
public class AppManager {

    private static AppManager manager;

    private Application application;
    private YFUserSession userSession;

    private Dialog pushDialog;

    private AppManager() {

    }

    public static AppManager getManager() {
        synchronized (AppManager.class) {
            if (manager == null) {
                manager = new AppManager();
            }
            return manager;
        }
    }

    public void initApplication(Application application) {
        this.application = application;

        BaseUtils.initContext(application);

        userSession = new YFUserSession(application);

        CrashHandler.init(application.getApplicationContext());
    }

    public Application getApplication() {
        return application;
    }

    public YFUserSession getUserSession() {
        return userSession;
    }

    /**
     *
     */
    public void pushOut() {
        final Activity topActivity = ActivityStack.topActivity();
        getUserSession().clearUserSession();
        if ((pushDialog == null || !pushDialog.isShowing()) && topActivity != null
                && !(topActivity instanceof LoginActivity)) {
            CustomDialog.Builder builder = new CustomDialog.Builder(topActivity);
            builder.setMessage("您的登录已过期，请重新登录！");
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    if (pushDialog != null) {
                        pushDialog.dismiss();
                    }
                    Intent intent = new Intent(topActivity, LoginActivity.class);
                    intent.putExtra("pushOut", true);
                    topActivity.startActivity(intent);
                    ActivityStack.finishAllExceptActivity(LoginActivity.class);
                }
            });

            pushDialog = builder.create();
            pushDialog.setCancelable(false);
            pushDialog.show();
        }

    }

    /**
     * 退出登录
     * @param userAccount 当前用户ID
     */
    public void logout(String userAccount) {
        getUserSession().clearUserSession();
        Activity topActivity = ActivityStack.topActivity();
        Intent intent;
        if (topActivity == null) {
            intent = new Intent(application, LoginActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("pushOut", false);
            intent.putExtra("userAccount",userAccount);
            application.startActivity(intent);
        } else {
            intent = new Intent(topActivity, LoginActivity.class);
            intent.putExtra("pushOut", false);
            intent.putExtra("userAccount",userAccount);
            topActivity.startActivity(intent);
        }
        ActivityStack.finishAllExceptActivity(LoginActivity.class);
    }

}
